<?php

/**
 * @author
 * @copyright 2014
 */
class User extends CI_Controller {

    function __construct() {
        parent::__construct();
		$this->load->model();
		$this->load->model(array('base_model'));
        //$this->lang->load('title');
    }
    function _remap($method, $params = array()) {
        if (method_exists($this, $method)) {
            return call_user_func_array(array($this, $method), $params);
        }
        $this->_view();
    }
    function _view() {
		$data = new stdClass();
        $this->load->helper('form');
        $data->page = 0;
        $title = "nhân viên"; 
		
		$controller = $this->uri->segment(1);
		$data->cotrol = $controller;
		$right = $this->login_model->permission($controller); 
		if(!isset($right['view'])){
			redirect(base_url().'home');
		}
		$data->rights = $right;
		
		$data->title = $title;
		$groupss = $this->base_model->getGroup();
		$data->groupss = form_dropdown('groups', $groupss,'', 'id="sgroups" style="width:170px; height:18px;" class="combo"');
        $content = $this->load->view('view', $data, true);
		$this->pub->write('title',$title,true);
		$this->pub->write('content',$content,true);
        $this->pub->render();
    }
	function getList(){
		 $data = new stdClass();
		 $result = new stdClass();
		 $counts = $this->pub->config['numrows'];
		 $page=$this->input->post('page');
		 $search = json_decode($this->input->post('search'));	
		 $controller = $this->uri->segment(1);
		 $right = $this->login_model->permission($controller); 
		 $data->rights = $right;
		 
		 $sql_search = "";
		 if(!empty($search->fullname)){
			$sql_search.= " and u.fullname like '%".($search->fullname)."%'";
		 }
		 if(!empty($search->groups)){
			$sql_search.= " and u.groups in(".($search->groups).") ";
		 }
		 $sql = "select u.*, g.groupname
				from users u
				left join groups g on u.groups = g.id
				where u.isdelete = 0
		";
		 $sql.= $sql_search;
		 $query_total = $this->model->query($sql)->execute();
		 $count = count($query_total);
		 if($page > 0){ 
			$page = $page.',';
		 }
		 else{
			$page = '';
		 }
		 $sql.= ' limit '.$page.$counts;
		 //echo $page; exit;
		 $query = $this->model->query($sql)->execute();
		 $page_view='&nbsp;';
		 if($count>0){
			$page_view=$this->pub->phantrang($count,$counts,5,'user/',$page);
		 }
         if(empty($page_view)) $page_view='&nbsp;';
         $result->paging = $page_view;
         $result->cPage = $page;
		 $data->datas = $query;
		 $result->content =  $this->load->view('list', $data, true);
		 echo json_encode($result);
	}
	function insert(){
		$this->load->helper('form');
		$data = new stdClass();
		$id =  $this->input->post('id');
		 
		$query = $this->model->table('users')
					  ->select('users.*,groups.groupname')	
					  ->join('groups','users.groups= groups.id','left')
					  ->where('users.id',$id)->find();
		 if(!empty($query->birthday) && $query->birthday != '0000-00-00'){
			$data->birthday = date('d-m-Y',strtotime($query->birthday));
		 }
		 else{
			$data->birthday = '';
		 }
		 $data->datas = $query;
		 $groupss = $this->base_model->getGroupCombo();
		 $group = ""; $sex = "";
		 if(!empty($query->groups)){
			$id = $query->id;
			$group = $query->groups;
			$sex = $query->sex;
		 }
		 $data->id = $id;
		 $data->sex = $sex;
		 $data->groupss = form_dropdown('groups', $groupss,$group, 'id="groups" style="width:160px;" class="insert groupss combo"');
		 echo $this->load->view('form', $data, true);	
	}
	function save(){
		$str = $this->input->post('str');
		$id = $this->input->post('id');	
		$sex = $this->input->post('sex');	
		$password = $this->input->post('password');
		if($str !='{}'){
			$json = json_decode($str);
			$data = array();
			foreach($json as $clm=>$val){
				$data[$clm] = $val;
			}
			//Kiểm tra trùng tài khoản
			$check = $this->checkDuplicate($id,$data['username']);
			if($check > 1){
				echo 2; exit;
			}
			$data['datecreate'] =  gmdate("Y-m-d H:s:i", time() + 7 * 3600);
			$data['usercreate'] = $this->pub->login['username'];
			$data['birthday'] = date('Y-m-d',strtotime($data['birthday']));		
			$data['sex'] = $sex;
			if(!empty($password)){
				$data['password'] = md5($password);
			}
			if(empty($id)){
				$this->model->insert($data);
			}
			else{
				$this->model->save($id,$data);
			}
			echo 1;
		}
		else{
			echo 0;	
		}
	}
	function checkDuplicate($id,$username){
		if(empty($id)){
			$data = $this->model->table('users')
								->select('username')
								->where('username',$username)
								->find();
			if(empty($data->username)){
				return 1;
			}
			else{
				return 2;
			}
		}
		else{
			$data = $this->model->table('users')
								->select('username')
								->where('id <>',$id)
								->where('username',$username)
								->find();
			if(empty($data->username)){
				return 1;
			}
			else{
				return 2;
			}		
		}
	}
	function delete(){
		$str = $this->input->post('str');
		$item = $this->input->post('item');
		if(!empty($item)){
			$this->model->where('depatmentid',$item)->update(array('isdelete'=>1));
			echo 1;
		}
		else{
			$array = explode(',',$str);
			foreach($array as $k=>$id){
				$this->model->where('depatmentid',$id)->update(array('isdelete'=>1));	
				echo 1;
			}
		}
	}
	function loadSearch(){
		$key = $this->input->post('key');
		$cartype = $this->model->where('isdelete',0)
							   ->select('fullname')	
							   ->like('fullname',$key) 
							   ->limit(10)
							   ->find_all();
		$str = "";
		foreach($cartype as $item){
			$str.= ','.($item->fullname).'';
		}
		$str = substr($str,1);
		echo $str; 
	}
}